package com.datamation.swadeshi.control;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.datamation.swadeshi.control.data.ReferenceDS;
import com.datamation.swadeshi.model.Reference;

public class ReferenceNum {
	Context context;

	public ReferenceNum(Context context) {
		this.context = context;
	}
	
	
	public String getCurrentRefNo(String cSettingsCode){
		
		ReferenceDS referenceDS=new ReferenceDS(context);
		
		String nextNumVal=referenceDS.getNextNumVal(cSettingsCode);
		
		ArrayList<Reference> list=referenceDS.getCurrentPreFix(cSettingsCode);
		String preFix = "";
		String refno = "";
		DecimalFormat dFormat = new DecimalFormat("000000");
		if(!nextNumVal.equals("")){
			
			for (Reference reference : list) {
				preFix=reference.getCharVal()+reference.getDealPreFix()+reference.getRepPrefix();
			}
			Log.v("next num val","NEXT :" +preFix+"/"+dFormat.format(Integer.valueOf(nextNumVal)));
			
		}else{
			for (Reference reference : list) {
				preFix=reference.getCharVal()+reference.getDealPreFix()+reference.getRepPrefix();
			}
			
			Log.v("next num val","NEXT :" +preFix+"/"+dFormat.format(Integer.valueOf(nextNumVal)));
			Log.v("next num val","NEXT :" +preFix);
		}
		
		return preFix+"/"+dFormat.format(Integer.valueOf(nextNumVal));	
	}
	
	
	public int nNumValueInsertOrUpdate(String cSettingsCode){
		
		ReferenceDS referenceDS=new ReferenceDS(context);
		int nextNumVal= Integer.parseInt(referenceDS.getNextNumVal(cSettingsCode))+1;
		int count=referenceDS.InsetOrUpdate(cSettingsCode, nextNumVal);
		
		if(count>0){
			Log.v("InsertOrUpdate","success");
		}else{
			Log.v("InsertOrUpdate","Failed");
		}
		
		return 0;
		
	}
}
